package com.lewis.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lewis.core.utils.DateUtil;
import com.lewis.core.utils.StringUtil;
import com.lewis.system.entity.SysLogLogin;
import com.lewis.system.mapper.SysLogLoginMapper;
import com.lewis.system.service.ISysLogLoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author Lewis
 */
@Service
public class SysLogLoginServiceImpl extends ServiceImpl<SysLogLoginMapper, SysLogLogin> implements ISysLogLoginService {

    @Resource
    private SysLogLoginMapper sysLogLoginMapper;

    @Override
    public List<SysLogLogin> selectLoginInForList(SysLogLogin sysLogLogin) {
        LambdaQueryWrapper<SysLogLogin> lqw = new LambdaQueryWrapper<SysLogLogin>()
                .like(StringUtil.isNotEmpty(sysLogLogin.getIpaddr()), SysLogLogin::getIpaddr, sysLogLogin.getIpaddr())
                .like(StringUtil.isNotEmpty(sysLogLogin.getUserName()), SysLogLogin::getUserName, sysLogLogin.getUserName())
                .eq(StringUtil.isNotEmpty(sysLogLogin.getStatus()), SysLogLogin::getStatus, sysLogLogin.getStatus())
                .orderByDesc(SysLogLogin::getLoginTime);
        Map<String, Object> params = sysLogLogin.getParams();
        if (params != null) {
            Object beginTime = sysLogLogin.getParams().get("beginTime");
            if (beginTime != null) {
                lqw.ge(StringUtil.isNotEmpty(String.valueOf(DateUtil.parseDate(sysLogLogin.getParams().get("beginTime")))), SysLogLogin::getLoginTime, String.valueOf(DateUtil.parseDate(sysLogLogin.getParams().get("beginTime"))));
            }
            Object endTime = sysLogLogin.getParams().get("endTime");
            if (endTime != null) {
                lqw.le(StringUtil.isNotEmpty(String.valueOf(DateUtil.parseDate(sysLogLogin.getParams().get("endTime")))), SysLogLogin::getLoginTime, String.valueOf(DateUtil.parseDate(sysLogLogin.getParams().get("endTime"))));
            }
        }
        return sysLogLoginMapper.selectList(lqw);
    }

}
